<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form :model="listQF" inline>
        <el-form-item label="物料名称">
          <el-input
            v-model="listQF.materialName"
            placeholder="物料名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="型号">
          <el-input
            v-model="listQF.materialModel"
            placeholder="型号"
          ></el-input>
        </el-form-item>
        <el-form-item label="规格">
          <el-input
            v-model="listQF.materialSpecification"
            placeholder="规格"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">查询</el-button>
          <el-button @click="ResetList">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        style="margin-top: 20px"
        :header-cell-style="{ background: '#f8f8f8' }"
        :data="list"
        @selection-change="selectionChange"
      >
        <el-table-column type="selection"></el-table-column>
        <el-table-column
          v-for="item in listColumn"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          v-slot="{ row }"
          width="280px"
        >
          <div>
            <el-button size="mini" type="success" @click="OpenMoveDialog(row)"
              >移库</el-button
            >
            <el-button type="primary" size="mini" @click="OpenEditDialog(row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDel(false, row.id)"
              >删除</el-button
            >
          </div>
        </el-table-column>
      </el-table>
      <el-pagination
        style="display: flex; justify-content: right"
        @size-change="PagerLimitChange"
        @current-change="PagerPageChange"
        :current-page="pager.page"
        :page-sizes="[2, 10, 30, 40]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      center
      width="1200px"
      :visible="visible"
      :title="title"
      @close="close"
      :close-on-click-modal="false"
    >
      <el-form inline ref="listRef" :model="listEF">
        <el-form-item label="物料名称" prop="materialId">
          <el-select v-model="listEF.materialId" placeholder="物料名称">
            <el-option
              v-for="item in materialList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="仓库" prop="warehouseId">
          <el-select
            @change="warehouseChange"
            v-model="listEF.warehouseId"
            placeholder="仓库"
          >
            <el-option
              v-for="item in warehouseList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存上限" prop="upperLimit">
          <el-input-number
            v-model="listEF.upperLimit"
            size="mini"
            :min="1"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="库存下限" prop="lowerLimit">
          <el-input-number
            v-model="listEF.lowerLimit"
            size="mini"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>
      <el-table :data="detailList">
        <el-table-column label="库区" v-slot="{ row }">
          <el-select
            @change="areaChange(listEF.warehouseId, row.area.id)"
            v-model="row.area.id"
            placeholder="库区"
          >
            <el-option
              v-for="item in areaList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-table-column>
        <el-table-column label="库位" v-slot="{ row }">
          <el-select v-model="row.location.id" placeholder="库位">
            <el-option
              v-for="item in loactionList(listEF.warehouseId, row.area.id) ||
              []"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-table-column>
        <el-table-column label="现有库存" v-slot="{ row }">
          <el-input-number
            size="mini"
            v-model="row.currentStock"
            :min="1"
          ></el-input-number>
        </el-table-column>
        <el-table-column label="生产日期" v-slot="{ row }">
          <el-input v-model="row.createTime"></el-input>
        </el-table-column>
        <el-table-column label="生产厂家" v-slot="{ row }">
          <el-input v-model="row.manufacturer"></el-input>
        </el-table-column>
        <el-table-column label="生产批号" v-slot="{ row }">
          <el-input v-model="row.batchNo"></el-input>
        </el-table-column>
        <el-table-column label="备注" v-slot="{ row }">
          <el-input v-model="row.remark"></el-input>
        </el-table-column>
      </el-table>
      <template #footer>
        <div>
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="handlesubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
      title="移库"
      :visible.sync="visibleMove"
      @close="CloseMoveDialog"
      center=""
    >
      <p style="text-align: center">您是否确定要移库吗？</p>
      <template #footer>
        <div>
          <el-button @click="CloseMoveDialog">取消</el-button>
          <el-button title="primary" @click="handleMove">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getCustomerList } from '@/api_zc/task/sale-order-approve'
import { getMaterialList, delMaterial, editMaterial, addMaterial } from '@/api_zc/mes/bom'
import { getMaterialCatgoryChildrenList } from '@/api_zc/wms/material-category'
import { getMaterialInventoryList, getMarehouseList, editInventory, delInventory } from '@/api_zc/wms/material-inventory'
export default {
  mounted () {
    this.getList()
    this.getMaterialCatgoryChildrenList()
    this.getUnity()
  },
  data () {
    return {
      pager: {
        limit: 10,
        page: 1,
        total: 0
      },
      list: [],
      listColumn: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '仓库',
          prop: 'warehouse.name'
        },
        {
          label: '物料编码',
          prop: 'material.code'
        },
        {
          label: '物料名称',
          prop: 'material.name'
        },
        {
          label: '物料型号',
          prop: 'material.model'
        },
        {
          label: '物料规格',
          prop: 'material.specification'
        },
        {
          label: '物料单价',
          prop: 'material.price'
        },
        {
          label: '总库存',
          prop: 'totalStock'
        },
        {
          label: '锁定库存',
          prop: 'lockedStock'
        },
        {
          label: '库存上限',
          prop: 'upperLimit'
        },
        {
          label: '库存下限',
          prop: 'lowerLimit'
        },
      ],
      listQF: {
        materialName: '',
        materialModel: '',
        materialSpecification: ''
      },
      listEF: {
        id: '',
        materialId: '',
        materialName: '',
        warehouseName: "",
        warehouseId: '',
        upperLimit: '',
        lowerLimit: '',
      },
      visible: false,
      title: '修改销售退货',
      ids: [],
      options: [],
      visibleMove: false,
      moveForm: {},
      detailList: [],
      materialList: [],
      warehouseList: [],
      areaList: [],
      loactionObj: {}
    }
  },
  methods: {
    async getUnity () {
      const data1 = await getMaterialList({ page: 1, limit: 999 })
      const data2 = await getMarehouseList()
      this.materialList = data1.data.content
      this.warehouseList = data2.data
    },
    OpenMoveDialog (row) {
      this.moveForm = row
      this.visibleMove = true
    },
    CloseMoveDialog () {
      this.visibleMove = false
    },
    async handleMove () {
      this.$message.success('移库成功')
      this.visibleMove = false
    },
    async getMaterialCatgoryChildrenList () {
      const { data } = await getMaterialCatgoryChildrenList()
      this.options = data
    },
    async getUnityList () {
      const { data: { content } } = await getCustomerList()
      this.customerList = content
    },
    async getList () {
      const { data: { total, content } } = await getMaterialInventoryList({ ...this.listQF, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    ResetList () {
      Object.assign(this.listQF, this.$options.data().listQF)
      this.getList()
    },
    PagerLimitChange (val) {
      this.pager.limit = val
      this.getList()
    },
    PagerPageChange (val) {
      this.pager.page = val
      this.getList()
    },
    OpenEditDialog (row) {
      this.isAdd = false
      this.visible = true
      this.$nextTick(() => {
        for (let key in row) {
          if (row.hasOwnProperty(key)) {
            if (this.listEF.hasOwnProperty(key)) {
              this.listEF[key] = row[key]
            }
          }
        }
      })
      this.listEF.warehouseId = row.id
      this.listEF.materialId = row.material.id
      this.detailList = row.detailList
      this.getAreaList(row.id)
      this.title = '修改物料档案管理'
    },
    getAreaList (id) {
      this.areaList = this.warehouseList.find(item => item.id == id).areaList
    },
    warehouseChange (val) {
      this.areaList = []
      this.detailList.forEach(item => {
        this.$set(item.area, 'id', null)
        this.$set(item.location, 'id', null)
      })
      this.getAreaList(val)
    },
    areaChange (id1, id2) {
      this.loactionObj = {}
      this.loactionList(id1, id2)
    },
    loactionList (id1, id2) {
      let list = this.warehouseList.find(item => item.id == id1).areaList.find(i => i.id == id2)?.locationList || []
      if (this.loactionObj.hasOwnProperty(id2)) {
        return this.loactionObj[id2]
      }
      this.$set(this.loactionObj, id2, list)
      return this.loactionObj[id2]
    },
    OpenAddDialog () {
      this.isAdd = true
      this.visible = true
      this.title = '添加物料档案管理'
      this.$nextTick(() => {
        for (let key in this.listEF) {
          this.listEF[key] = ''
        }
        this.$refs.listRef.resetFields()
      })
    },
    close () {
      this.visible = false
    },
    handleDel (status, id) {
      let ids = []
      if (status) {
        ids = this.ids
      } else {
        ids[0] = id
      }
      this.$confirm(`是否确认删除销售退货编号为${ids.join(',')}的数据项？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await delInventory({ ids })
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selectionChange (selectArray) {
      this.ids = selectArray.map(item => item.id)
    },
    handlesubmit () {
      this.$refs.listRef.validate(async (callback) => {
        if (!callback) {
          return
        }
        if (this.isAdd) {
          await addMaterial({ ...this.listEF, })
          this.$message.success('新建成功')
        } else {
          this.detailList.forEach(item => {
            item.productionDate = new Date(item.productionDate)
          })
          await editInventory({ ...this.listEF, detailList: this.detailList })
          this.$message.success('修改成功')
        }
        this.visible = false
        this.getList()
        this.$refs.listRef.resetFields()
      })
    },
  },
}
</script>

<style>
</style>